package com.demo.uuid;

import cn.hutool.core.util.IdUtil;
import com.fasterxml.uuid.Generators;
import com.fasterxml.uuid.impl.NameBasedGenerator;
import com.fasterxml.uuid.impl.RandomBasedGenerator;
import com.fasterxml.uuid.impl.TimeBasedGenerator;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;

import java.util.UUID;

public class UuidGenerate {
    @Test
    public void javaGenerate() {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        System.out.println(uuid);
    }

    @Test
    public void commonsLang3() {
        String randomUUID = RandomStringUtils.random(32, true, true);
        System.out.println(randomUUID);
    }

    // JUG 库很好
    @Test
    public void JUGGenerator() {
        // 版本1：基于时间的UUID
        TimeBasedGenerator timeBasedGenerator = Generators.timeBasedGenerator();
        UUID timeBasedUUID = timeBasedGenerator.generate();
        System.out.println("版本1 UUID (基于时间): " + timeBasedUUID);

        // 版本4：完全随机的UUID
        RandomBasedGenerator randomBasedGenerator = Generators.randomBasedGenerator();
        UUID randomUUID = randomBasedGenerator.generate();
        System.out.println("版本4 UUID (随机): " + randomUUID);

        // 版本2/3：基于MD5散列的UUID
        UUID namespace = UUID.fromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8"); // DNS命名空间
        String name = "example";
        NameBasedGenerator md5Generator = Generators.nameBasedGenerator(NameBasedGenerator.NAMESPACE_DNS);
        UUID md5UUID = md5Generator.generate(name);
        System.out.println("版本3 UUID (基于MD5散列): " + md5UUID);

        // 版本5：基于SHA-1散列的UUID
        NameBasedGenerator sha1Generator = Generators.nameBasedGenerator(NameBasedGenerator.NAMESPACE_URL);
        UUID sha1UUID = sha1Generator.generate(name);
        System.out.println("版本5 UUID (基于SHA-1散列): " + sha1UUID);

    }

    @Test
    public void hutool(){
        // 生成一个UUID
        String uuid = IdUtil.randomUUID();
        System.out.println(uuid);
    }
}
